package com.smfx.yxt.controller.teacher;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.smfx.yxt.common.AppUser;
import com.smfx.yxt.common.Const;
import com.smfx.yxt.common.MenuConst;
import com.smfx.yxt.controller.BaseController;
import com.smfx.yxt.model.StudentInfo;
import com.smfx.yxt.reqVo.BaseQueryVO;
import com.smfx.yxt.reqVo.ScoreSummaryQueryVO;
import com.smfx.yxt.service.IClassTeachersService;
import com.smfx.yxt.service.IOrganizeGroupService;
import com.smfx.yxt.service.IScoreSummaryService;
import com.smfx.yxt.service.IStudentService;
import com.smfx.yxt.service.ITeacherService;
import com.smfx.yxt.utils.BeanUtils;
import com.smfx.yxt.utils.StringUtil;
import com.smfx.yxt.view.StudentInfoView;
import com.smfx.yxt.view.TeacherInfoView;

@RequestMapping(Const.TEACHER_ROLE +  MenuConst.SCORE_REPORT)
@Controller
public class ScoreReportController extends BaseController{

	private Logger logger = LoggerFactory.getLogger(ScoreReportController.class);
	@Autowired
	private ITeacherService teacherService;
	@Autowired
	private IStudentService studentService;
	@Autowired
	private IScoreSummaryService scoreSummaryService;
	@Autowired
	private IClassTeachersService classTeachersService;
	@Autowired
	private IOrganizeGroupService organizeGroupService;
	
	
	@RequestMapping("student")
	public String studentList(Model model) {
		AppUser appUser = getAppUser();
		BaseQueryVO vo = new BaseQueryVO();
		vo.setFinishYear(getCurrentTerm().getFinishYear());
		vo.setId(appUser.getTeacher().getId());
		List<TeacherInfoView> teacherInfoView = teacherService.selectClassForTeacher(vo);
		if(!StringUtil.isNullOrEmpty(teacherInfoView)) {
			model.addAttribute("model", teacherInfoView.get(0));
		}
		int count = organizeGroupService.isLeaderGroup(appUser.getTeacher().getId());
		model.addAttribute("isleader", count);
		return "teacher/scoreReport/studentList";
	}
	
	@RequestMapping("clazz")
	public String clazzList() {
		return "teacher/scoreReport/clazzList";
	}
	
	@RequestMapping("studentScore/{studentId}")
	public String studentScore(@PathVariable Integer studentId, Model model) {
		StudentInfo student = studentService.selectById(studentId);
		if(student == null) return null;
		model.addAttribute("student", BeanUtils.copyAs(student, StudentInfoView.class));
		if(getCurrentTerm().getTermKey() - student.getGrade() > 3 ) {
			AppUser appUser = getAppUser();
			int count = organizeGroupService.isLeaderGroup(appUser.getTeacher().getId());
			if(count == 0) return null;
		}
		return "teacher/scoreReport/studentScore";
	}
	
	@RequestMapping("clazzScore/{grade}/{clazz}")
	public String clazzScore(@PathVariable Integer grade, 
			@PathVariable Integer clazz, Model model) {
		model.addAttribute("grade", grade);
		model.addAttribute("clazz", clazz);
		return "teacher/scoreReport/clazzScore";
	}
	
	@RequestMapping("getClassTeachers")
	@ResponseBody Object getClassTeachers(BaseQueryVO vo) {
		try {
			AppUser user = getAppUser();
			vo.setSchoolId(user.getTeacher().getSchoolId());
			return classTeachersService.selectByClassTeachers(vo);
		} catch (Exception e) {
			logger.error("--clazzScoreList error={}", e);
		}
		return null;
	}
	
	@RequestMapping("clazzScore/list")
	@ResponseBody Object clazzScoreList(ScoreSummaryQueryVO vo) {
		try {
			return scoreSummaryService.selectScoreSummaryList(vo);
		} catch (Exception e) {
			logger.error("--clazzScoreList error={}", e);
		}
		return null;
	}
	
	@RequestMapping("clazzStudentScore/list")
	@ResponseBody Object clazzStudentScoreList(ScoreSummaryQueryVO vo) {
		try {
			int grade = 2012;
			int termKey = getCurrentTerm().getTermKey().intValue();
			int num = termKey - grade;
			List<Integer> list = new ArrayList<>();
			for(int i = 0; i <= num; i++) {
				list.add(grade + i);
			}
			vo.setScoreList(list);
			return scoreSummaryService.selectClazzStudentScoreList(vo);
		} catch (Exception e) {
			logger.error("---clazzStudentScoreList error={}", e);
		}
		return null;
	}
	
	@RequestMapping("finalTermScore")
	public String finalTermScore() {
		return "teacher/finalTermScore/studentList";
	}
	
}
